import { reactive } from 'vue';
import { saveItem, removeItem, getItem } from '@/utils/util';
// #ifdef H5
import nativeApi from './nativeApi'
// #endif


export const systemInfo = reactive({
  statusBarHeight: 0, // 状态导航栏高度
  navigationBarHeight: 0, // 导航栏高度(标题栏高度)
  safeBottom: 0, // 底部安全距离
  navHeight: 0, // 总体高度=状态导航栏高度+导航栏高度(标题栏高度)
  windowHeight: 0, // 可使用窗口高度,
  windowWidth: 0,
  screenHeight: 0, // 屏幕高度,
  menuWidth: 0, // 小程序胶囊宽度
});

export const getSystemInfo = () => {
  return systemInfo;
};

export const setSystemInfo = () => {
  const info = uni.getSystemInfoSync();
  const safeArea = info.safeArea;
  const bottom = safeArea?.bottom || 0;
  const height = Number(safeArea?.height) + Number(safeArea?.top) || 0;
  console.log('info---', info);
  console.log('safeArea---', safeArea);
  systemInfo.windowWidth = safeArea?.width || 0;

  // #ifdef MP-WEIXIN
  //获取胶囊信息
  const menu = uni.getMenuButtonBoundingClientRect();
  systemInfo.menuWidth = menu.width;
  console.log('memu---', menu);

  //计算组件高度
  systemInfo.statusBarHeight = info.statusBarHeight || 0; //状态栏高度
  //导航栏高度= （胶囊顶部距离-状态栏高度） x 2 + 胶囊的高度
  systemInfo.navigationBarHeight = (menu.top - systemInfo.statusBarHeight) * 2 + menu.height;
  //总高度 = 状态栏的高度 + 导航栏高度
  systemInfo.navHeight = systemInfo.statusBarHeight + systemInfo.navigationBarHeight;

  systemInfo.screenHeight = info.screenHeight; // 屏幕高度
  if (info.osName === 'ios') {
    systemInfo.safeBottom = info.screenHeight - bottom; // 底部安全距离=屏幕高度-安全区域右下角纵坐标
  } else {
    systemInfo.safeBottom = bottom - height;
  }
  systemInfo.windowHeight =
    info.windowHeight - systemInfo.safeBottom - (menu.height - systemInfo.statusBarHeight); // 可使用窗口高度
  // #endif

  // #ifdef H5
  console.log("nativeApi------getStatusHeight", nativeApi.getStatusHeight());
  console.log("nativeApi------getSafeAreaHeight", nativeApi.getSafeAreaHeight());
  systemInfo.statusBarHeight = nativeApi.getStatusHeight() || 0; // 状态栏高度
  systemInfo.navigationBarHeight = 44; // 导航栏高度
  systemInfo.navHeight = nativeApi.getStatusHeight() + 44;  // 状态栏高度 + 导航栏高度
  systemInfo.screenHeight = info.windowHeight;
  systemInfo.safeBottom = nativeApi.getSafeAreaHeight() || 0;// 底部安全距离
  systemInfo.windowHeight = info.screenHeight - systemInfo.safeBottom - systemInfo.navHeight;
  // #endif

  console.log('info---------', systemInfo);
};

let statusBarStyle = ref('');
/** 状态栏设置 */
export const getStatusBarStyle = (color: string = '#fff') => {
  statusBarStyle.value = `width: 100vw; height: ${systemInfo.statusBarHeight}px; background: ${color};`;
  return statusBarStyle.value;
};

let navigationBarStyle = ref('');
/** 导航栏设置 */
export const getNavigationBarStyle = (color: string = '#fff') => {
  navigationBarStyle.value = `width: 100vw; height: ${systemInfo.navigationBarHeight}px; background: ${color};`;
  return navigationBarStyle.value;
};

let navigationBarStyle1 = ref('');
/** 导航栏设置1 */
export const getNavigationBarStyle1 = (color: string = '#fff') => {
  navigationBarStyle1.value = `width: 100vw; height: ${systemInfo.navigationBarHeight}px; background: ${color}; margin-top: ${systemInfo.statusBarHeight}px;`;
  return navigationBarStyle1.value;
};

let fullSHStyle = ref('');
/** 全屏背景 */
export const getFullSHStyle = (color: string = '#fff') => {
  fullSHStyle.value = `width: 100vw; height: ${systemInfo.windowHeight + systemInfo.navHeight
    }px; background: ${color};`;
  // #ifdef H5
  fullSHStyle.value = `width: 100%; height: 100vh; background: ${color};`;
  // #endif
  return fullSHStyle.value;
};

export const getwv = (w: number) => {
  const wv = (w * systemInfo.windowWidth) / 375
  try {
    return parseFloat(wv.toFixed(0))
  }
  catch (e) {
    return wv
  }
}

// 换肤颜色
export const colors = {
  systemColor: '#5073F7',
};

// token
export let token = ref('');
export const getToken = () => {
  if (token.value) {
    return token.value;
  } else {
    return getItem('token') || '';
  }
};
export const setToken = (val: string) => {
  token.value = val;
  saveItem('token', val);
};

export const clearToken = () => {
  token.value = '';
  removeItem('token');
};

// userid
export let userId = ref('');
export const getUserId = () => {
  if (userId.value) {
    return userId.value;
  } else {
    return getItem('userid') || '';
  }
};
export const setUserId = (val: string) => {
  userId.value = val;
  saveItem('userid', val);
};

export const clearUserId = () => {
  userId.value = '';
  removeItem('userid');
};

// openid
export let openId = ref('');
export const getOpenId = () => {
  if (openId.value) {
    return openId.value;
  } else {
    return getItem('openid') || '';
  }
};
export const setOpenId = (val: string) => {
  openId.value = val;
  saveItem('openid', val);
};

export const clearOpenId = () => {
  openId.value = '';
  removeItem('openid');
};

// 是否多充用户
export let moreCharge = ref(false);
export const getMoreCharge = () => {
  if (moreCharge.value) {
    return moreCharge.value;
  } else {
    return getItem('morecharge') || '';
  }
};
export const setMoreCharge = (val: boolean) => {
  moreCharge.value = val;
  saveItem('morecharge', val);
};

export const clearMoreCharge = () => {
  moreCharge.value = false;
  removeItem('morecharge');
};

let urlCode = ref('');
export const setUrlCode = (val: string) => {
  urlCode.value = val;
};
export const getUrlCode = () => {
  return urlCode.value;
};

// 扫码领券的活动id
let activityId = ref('');
export const setActivityId = (val: string) => {
  activityId.value = val;
};
export const getActivityId = () => {
  return activityId.value;
};

let scancode = ref('0');
export const setScancode = (val: string) => {
  scancode.value = val;
};
export const getScancode = () => {
  return scancode.value;
};

let adPopup = ref(true);
export const setAdPopup = (val: boolean) => {
  adPopup.value = val;
};
export const getAdPopup = () => {
  return adPopup.value;
};

let searchData: Array<string> = reactive([])
export const setSearchData = (str: string) => {
  const obj = searchData.findIndex((item: string) => item === str);
  if (obj > -1) {
    searchData.splice(obj, 1)
  }
  searchData.unshift(str);
  if (searchData.length > 5) {
    searchData.pop();
  }
  saveItem('searchData', JSON.stringify(searchData));
}
export const getSearchData = () => {
  if (searchData.length === 0) {
    const str = getItem('searchData');
    searchData = str ? JSON.parse(str) : [];
  }
  return searchData;
}
export const removeSearchData = () => {
  searchData = [];
  removeItem('searchData');
}

// 充电页面跳转传参数
let orderObj = reactive(undefined as any)
export const setOrderObj = (obj: any) => {
  orderObj = obj;
}
export const getOrderObj = () => {
  return orderObj
}

// 微信小程序toast相关逻辑
let toastInstance: any = reactive([])
export const setToastInstance = (vm: any) => {
  const currentPages = getCurrentPages();
  const currentPageRoute = currentPages[currentPages.length - 1]?.route;
  let hasToastKey = false
  toastInstance = toastInstance.map((item: any) => {
    if (item.key === currentPageRoute) {
      item.value = vm
      hasToastKey = true
    }
    return item
  })
  if (!hasToastKey) {
    toastInstance.push({ key: currentPageRoute, value: vm })
  }
}
export const getToastInstance = () => {
  const currentPages = getCurrentPages();
  const currentPageRoute = currentPages[currentPages.length - 1]?.route;
  const findVal = toastInstance.find((item: any) => {
    return item.key === currentPageRoute
  });
  return findVal.value
}

// h5 app第一次弹出定位权限弹窗  0是  1否
let locationPermissionModal = ref("0")
export const setLocationPermissionModal = (val: string) => {
  locationPermissionModal.value = val
}
export const getLocationPermissionModal = () => {
  return locationPermissionModal.value
}

// h5地图实例
let aMapInstance: any = ref();
export const setAMapInstance = (val: any) => {
  aMapInstance.value = val
}
export const getAMapInstance = () => {
  return aMapInstance.value
}
